//
//  GoodsTableViewCell.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/24.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "GoodsTableViewCell.h"
#import "XHStarRateView.h"


@interface GoodsTableViewCell ()

@property (nonatomic, weak)XHStarRateView * starView;
@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * shopImageView;
@property (nonatomic, weak)UILabel * goodsNameLabel;
@property (nonatomic, strong)UILabel * shopNameLabel;
@property (nonatomic, strong)UILabel * localLabel;
@property (nonatomic, strong)UILabel * dateLabel;
@property (nonatomic, weak)UILabel * moneyLabel;
@property (nonatomic, weak)UIView * smallView;
@property (nonatomic, weak)UILabel * originalLabel;
@property (nonatomic, weak)UILabel * commentLabel;
@property (nonatomic, weak)UILabel * stateLabel;
@property (nonatomic, weak)UIImageView * tuanImageView;

@end

@implementation GoodsTableViewCell


- (void)setModel:(GoodsModel *)model{
    
    _model = model;
    
    self.goodsNameLabel.text = model.merchants_goods_name;
    int count = [model.merchants_goods_comments_star_level intValue];
    if (count == 0) {
        self.starView.width = 1 * SCREENH6;;
        self.commentLabel.hidden = NO;
    }else{
        self.starView.currentRating = count;
        self.commentLabel.hidden = YES;
    }
    self.shopNameLabel.text = model.merchants_name;
    NSArray *array = [model.merchants_goods_img componentsSeparatedByString:@","];
    NSString * img = array.firstObject;
    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,img];
    NSURL * url = [NSURL URLWithString:usrlStr];
    [self.shopImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"组12"]];
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",model.merchants_goods_discount_price];
//    self.originalLabel.text = [NSString stringWithFormat:@"原价 ￥%@",model.merchants_goods_price];
    NSString * orString = [NSString stringWithFormat:@"￥%@",model.merchants_goods_price];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:orString attributes:attribtDic];
    
    self.originalLabel.attributedText = attribtStr;
    
    int merchants_goods_status = [model.merchants_goods_status intValue];
    if (merchants_goods_status == 0) {
        
        self.stateLabel.hidden = NO;
        self.editButton.hidden = YES;
        self.updateButton.hidden = YES;
        self.stateLabel.text = @"审核中";
    }else if (merchants_goods_status == 1){
        
        self.stateLabel.hidden = YES;
        self.updateButton.hidden = NO;
        self.editButton.hidden = NO;
        self.editButton.selected = YES;
        [self.editButton setBackgroundColor:[UIColor lightGrayColor]];
    }else if (merchants_goods_status == 2){
        
        self.stateLabel.hidden = YES;
        self.editButton.hidden = NO;
        self.updateButton.hidden = YES;
        self.editButton.selected = NO;
    }else if (merchants_goods_status == 3){
        
        self.stateLabel.hidden = YES;
        self.updateButton.hidden = NO;
        self.editButton.hidden = NO;
        self.editButton.selected = YES;        
        [self.editButton setBackgroundColor:[UIColor lightGrayColor]];
    }else if (merchants_goods_status == 4){
        
        self.stateLabel.hidden = NO;
        self.editButton.hidden = YES;
        self.stateLabel.text = @"已删除";
    }

    int merchants_goods_type = [model.merchants_goods_type intValue];
    if (merchants_goods_type == 1) {
        
        self.tuanImageView.hidden = YES;
        self.dateLabel.text = [NSString stringWithFormat:@"%@",model.merchants_goods_available_time];
    }else{
        
        self.tuanImageView.hidden = NO;
        NSDictionary * dict = [self dictionaryWithJsonString:model.merchants_goods_available_time];
        NSArray *keysArray = [dict allKeys];
        NSString * key = keysArray.firstObject;
        NSArray * value = dict[key];
        NSString *string = [value componentsJoinedByString:@","];
        NSString * weekday;
        int week = [key intValue];
        switch (week) {
            case 1:
                weekday = @"周一";
                break;
            case 2:
                weekday = @"周二";
                break;
            case 3:
                weekday = @"周三";
                break;
            case 4:
                weekday = @"周四";
                break;
            case 5:
                weekday = @"周五";
                break;
            case 6:
                weekday = @"周六";
                break;
            case 7:
                weekday = @"周日";
                break;
                
            default:
                break;
        }
        self.dateLabel.text = [NSString stringWithFormat:@"%@ %@",weekday,string];
    }
    
    self.ID = [NSString stringWithFormat:@"%@",model.id];
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF8F9FA);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UIImageView * shopImageView = [UIImageView new];
    
    shopImageView.backgroundColor = [UIColor lightGrayColor];
    
    self.shopImageView = shopImageView;
    
    [self.backView addSubview:self.shopImageView];
    
    UILabel * goodNameLabel = [UILabel new];
    
    goodNameLabel.font = [UIFont boldSystemFontOfSize:14 * SCREENH6];
    goodNameLabel.text = @"商品名";
    goodNameLabel.textColor = ZRBlackColor;
    
    self.goodsNameLabel = goodNameLabel;
    
    [self.backView addSubview:self.goodsNameLabel];
    
    
    XHStarRateView *starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(75 * SCREENH6, 27 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    starRateView.isAnimation = YES; // 有动画
    starRateView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    starRateView.currentRating = 5;
    starRateView.userInteractionEnabled = NO;
    
    self.starView = starRateView;
    [self.backView addSubview:self.starView];
    
    
    self.shopNameLabel = [self setupLabelWithTitle:@"店铺名"];
    [self.backView addSubview:self.shopNameLabel];
    self.localLabel = [self setupLabelWithTitle:@"成都天府路"];
    [self.backView addSubview:self.localLabel];
    self.dateLabel = [self setupLabelWithTitle:@"9月21日- 11月20日"];
    [self.backView addSubview:self.dateLabel];
    
    
    UIView * smallView = [UIView new];
    
    smallView.backgroundColor = ColorWithRGB(0xFFE055);
    
    self.smallView = smallView;
    
    [self.backView addSubview:self.smallView];
    
    UILabel * moneyLabel = [UILabel new];
    
    moneyLabel.textColor = ZRBlackColor;
    moneyLabel.text = @"$ 20.9";
    moneyLabel.font = [UIFont boldSystemFontOfSize:14 * SCREENH6];
    
    self.moneyLabel = moneyLabel;
    
    [self.backView addSubview:self.moneyLabel];
    
    
    UILabel * originalLabel = [UILabel new];
    
    originalLabel.textColor = ZRBlackColor;
    originalLabel.text = @"原价 $30.2";
    originalLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    self.originalLabel = originalLabel;
    
    [self.backView addSubview:self.originalLabel];
    
    UIButton * editButton = [UIButton new];
    
    [editButton setTitle:@"下架" forState:UIControlStateNormal];
    [editButton setTitle:@"编辑" forState:UIControlStateSelected];
    [editButton setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [editButton setBackgroundColor:ZRBlackColor];
    [editButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    editButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.editButton = editButton;
    
    [self.backView addSubview:self.editButton];
    
    
    UIButton * updateButton = [UIButton new];
    
    [updateButton setTitle:@"上架" forState:UIControlStateNormal];
    [updateButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [updateButton setBackgroundColor:ColorWithRGB(0xFFE055)];
    [updateButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    updateButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    updateButton.hidden = YES;
    
    self.updateButton = updateButton;
    
    [self.backView addSubview:self.updateButton];
    
    
    UILabel * commitLabel = [UILabel new];
    
    commitLabel.textColor = [UIColor lightGrayColor];
    commitLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    commitLabel.text = @"暂无评论";
    commitLabel.hidden = YES;
    
    self.commentLabel = commitLabel;
    
    [self.backView addSubview:self.commentLabel];
    
    
    UILabel * stateLabel = [UILabel new];
    
    stateLabel.textColor = ColorWithRGB(0x53d499);
    stateLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    stateLabel.text = @"----";
    
    self.stateLabel = stateLabel;
    
    [self.backView addSubview:self.stateLabel];
    
    UIImageView * tuanImageView = [UIImageView new];
    
    tuanImageView.image = [UIImage imageNamed:@"首页拼团"];
    tuanImageView.hidden = YES;
    
    self.tuanImageView = tuanImageView;
    
    [self.contentView addSubview:self.tuanImageView];
}

- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(125 * SCREENH6);
    }];
    
    [self.shopImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.width.mas_equalTo(58 * SCREENH6);
        make.height.mas_equalTo(60 * SCREENH6);
    }];
    
    [self.goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shopImageView.mas_trailing).offset(10 * SCREENH6);
        make.width.mas_equalTo(200 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(8 * SCREENH6);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsNameLabel);
        make.top.mas_equalTo(self.backView).offset(25 * SCREENH6);
    }];
    
    [self.shopNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView);
        make.width.mas_equalTo(200 * SCREENH6);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shopNameLabel);
        make.width.mas_equalTo(200 * SCREENH6);
        make.top.mas_equalTo(self.shopNameLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.localLabel);
        make.width.mas_equalTo(190 * SCREENH6);
        make.top.mas_equalTo(self.localLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.tuanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView);
        make.centerY.mas_equalTo(self.dateLabel);
        make.width.mas_equalTo(45 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(8 * SCREENH6);
        make.height.mas_equalTo(10 * SCREENH6);
        make.width.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.originalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.moneyLabel.mas_trailing).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(4 * SCREENH6);
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.width.mas_equalTo(40 * SCREENH6);
        make.height.mas_equalTo(22 * SCREENH6);
    }];
    
    [self.updateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.editButton);
        make.top.mas_equalTo(self.editButton.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(10 * SCREENH6);
    }];
}


- (void)clickButton:(UIButton *)button{
    
//    NSString * name;
//
//    if(button == self.editButton){
//
//        if (button.selected) {
//
//            name = @"edit";
//            [button setBackgroundColor:ZRBlackColor];
//            button.selected = NO;
//        }else{
//
//            name = @"putDown";
//            [button setBackgroundColor:[UIColor lightGrayColor]];
//            button.selected = YES;
//            self.updateButton.hidden = NO;
//        }
//    }else{
//
//        name = @"update";
//
//        button.hidden = YES;
//        self.editButton.backgroundColor = ZRBlackColor;
//        self.editButton.selected = NO;
//    }
    
    
    if (self.goodsTableViewCellDelegate && [self.goodsTableViewCellDelegate respondsToSelector:@selector(clickItemButton: WithCell:)])
    {
        [self.goodsTableViewCellDelegate clickItemButton:button WithCell:self];
    }
}


- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.font = [UIFont systemFontOfSize:14 * SCREENH6];
    label.textColor = ZRBlackColor;
    
    return label;
}


- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
